<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 23:51
 */

namespace App\Models\Api;

use App\Models\Api\Base;
use Cache;
use Illuminate\Database\Eloquent\Model;

/**
 * 文章点赞模型
 * Class ArticleGood
 * @package App\Models\Api\Base
 */
class AstroPhaseText extends Base
{

    protected $table = 'astro_phase_text';

    public function getAllPhaseText()
    {
        $phase_text = false;

        if (intval($this->cache_on) >= 1 && Cache::has('astro_phase_text'))
        {
            $phase_text = Cache::get('astro_phase_text');
        }
        if ($phase_text === false)
        {
            try{
                $phase_text = $this->select('planet1','planet2','degrees','text')
                    ->where('status', 1)
                    ->get();

                $phase_text = json_decode(json_encode($phase_text, true), true);

                if ($this->cache_on >= 1)
                {
                    Cache::set('astro_phase_text', $phase_text, 86400 * 3);
                }
            }
            catch (\Exception $e)
            {
                $phase_text = [];
            }
        }
        return $phase_text;
    }

    public function getPhaseText($map = [])
    {
        $planet1_id = $map['planet1_id'];
        $planet2_id = $map['planet2_id'];
        $degrees = $map['degrees'];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has('astro_phase_text_' . $planet1_id . '-' . $planet2_id . '-' . $degrees))
        {
            if ($planet1_id < $planet2_id)
            {
                $data = Cache::get('astro_phase_text_' . $planet1_id . '-' . $planet2_id . '-' . $degrees);
            } else
            {
                $data = Cache::get('astro_phase_text_' . $planet2_id . '-' . $planet1_id . '-' . $degrees);
            }
        }
        if ($data === false)
        {
            if ($planet1_id < $planet2_id)
            {
                $phase_text = $this->select('text')->where(['status' => 1, 'planet1' => $planet1_id, 'planet2' => $planet2_id, 'degrees' => $degrees])->get();
            } else
            {
                $phase_text = $this->select('text')->where(['status' => 1, 'planet1' => $planet2_id, 'planet2' => $planet1_id, 'degrees' => $degrees])->get();
            }

            $phase_text = json_decode(json_encode($phase_text, true), true);

            if ($phase_text || !empty($phase_text))
            {
                foreach ($phase_text as $v)
                {
                    $data[] = $v['text'];
                }
            }
            Cache::set('astro_phase_text_' . $planet1_id . '-' . $planet2_id . '-' . $degrees, $data, 86400 * 3);
        }
        return $data;
    }

}
